home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / g_duke / mapprt.zip / MGRMAP.C < prev    next >
C/C++ Source or Header  |  1996-06-16  |  11KB  |  451 lines

  1. /*
  2.  
  3.    MGRMAP.C
  4.  
  5.    Oliver Kraus
  6.    kraus@lrs.e-technik.uni-erlangen.de
  7.  
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include "names.h"
  14. #include "bmf.h"
  15. #include "mgrmap.h"
  16.  
  17. mgrmap_type mgrmap_Open(char *fname, FILE *fp)
  18. {
  19.    mgrmap_type mm;
  20.    mm = (mgrmap_type)malloc(sizeof(mgrmap_struct));
  21.    if ( mm != NULL )
  22.    {
  23.       mm->map = map_Open(fname, fp);
  24.       if ( mm->map != NULL )
  25.       {
  26.          mm->mgr = NULL;
  27.          mm->dpi = 300;
  28.          mm->dest_width = 800L;
  29.          mm->dest_height = 600L;
  30.          mm->ratio_num = 1L;
  31.          mm->ratio_den = 1L;
  32.          mm->upper_skip = 0;
  33.          mm->idf_height = 15;
  34.          mm->tf_height = 30;
  35.          mm->lf_height = 22;
  36.          mm->is_monster = 1;
  37.          mm->is_card = 1;
  38.          return mm;
  39.       }
  40.       free(mm);
  41.    }
  42.    return NULL;
  43. }
  44.  
  45. void mgrmap_Close(mgrmap_type mm)
  46. {
  47.    if ( mm != NULL )
  48.    {
  49.       if ( mm->map != NULL )
  50.          map_Close(mm->map);
  51.       if ( mm->mgr != NULL )
  52.          mgr_Close(mm->mgr);
  53.       free(mm);
  54.    }
  55. }
  56.  
  57. void mgrmap_SetDestSize(mgrmap_type mm, long dest_width, long dest_height)
  58. {
  59.    assert(mm != NULL);
  60.    mm->dest_width = dest_width;
  61.    mm->dest_height = dest_height;
  62. }
  63.  
  64. void mgrmap_SetFontHeight(mgrmap_type mm, long idf_height, long tf_height, long lf_height)
  65. {
  66.    assert(mm != NULL);
  67.    mm->idf_height = idf_height;
  68.    mm->tf_height = tf_height;
  69.    mm->lf_height = lf_height;
  70. }
  71.  
  72.  
  73. #define mgrmap_XToMap(mm, x) \
  74.    (mm->x_skip+((((x)-(mm)->x_offset)*(mm)->ratio_num)/(mm)->ratio_den))
  75. #define mgrmap_YToMap(mm, y) \
  76.    ((mm)->mgr->height-(mm)->upper_skip-(((y)-(mm)->y_offset)*(mm)->ratio_num)/(mm)->ratio_den)
  77.  
  78.  
  79. int mgrmap_AllocMGR(mgrmap_type mm)
  80. {
  81.    long dest_height_sub;
  82.    int idx;
  83.    bmf_type bmf;
  84.    assert(mm != NULL);
  85.    if ( mm->map == NULL )
  86.       return 0;
  87.  
  88.    map_CalculateSize(mm->map);
  89.    map_cnt_sprites(mm->map);
  90.  
  91.    bmf = bmfa_MatchMaxFont(mm->lf_height);
  92.  
  93.    idx = 0;
  94.  
  95.    mm->legend_cnt = 0;
  96.    mm->legend_f1_size = 0L;
  97.    mm->legend_f2_size = 0L;
  98.    mm->legend_f3_size = 0L;
  99.  
  100.    while( map_get_item_typ(idx) >= 0 )
  101.    {
  102.       if ( map_get_item_cnt(idx) > 0 )
  103.       {
  104.          static char s[80];
  105.          long width;
  106.  
  107.          mm->legend_cnt++;
  108.  
  109.          sprintf(s, "%ldx-", map_get_item_cnt(idx));
  110.          width = bmf_GetStrWidth(bmf, s);
  111.  
  112.          if ( mm->legend_f1_size < width )
  113.             mm->legend_f1_size = width;
  114.  
  115.          sprintf(s, "%s-", map_get_item_id(idx));
  116.          width = bmf_GetStrWidth(bmf, s);
  117.  
  118.          if ( mm->legend_f2_size < width )
  119.             mm->legend_f2_size = width;
  120.  
  121.          sprintf(s, "%s--", map_get_item_string(idx));
  122.          width = bmf_GetStrWidth(bmf, s);
  123.  
  124.          if ( mm->legend_f3_size < width )
  125.             mm->legend_f3_size = width;
  126.       }
  127.       idx++;
  128.    }
  129.  
  130.    mm->legend_cols = 1;
  131.    mm->legend_rows = 0;
  132.    if ( mm->legend_cnt > 0 )
  133.    {
  134.       mm->legend_cols = mm->dest_width
  135.          /(mm->legend_f1_size+mm->legend_f2_size+mm->legend_f3_size);
  136.       if ( mm->legend_cols <= 0 )
  137.          mm->legend_cols = 1;
  138.       mm->legend_rows = ((mm->legend_cnt+mm->legend_cols-1)/(mm->legend_cols));
  139.    }
  140.  
  141.    bmf = bmfa_MatchMaxFont(mm->tf_height);
  142.  
  143.    dest_height_sub = 0L;
  144.    dest_height_sub += bmf_GetHeight(bmf);
  145.    dest_height_sub += bmf_GetHeight(bmf)/2;
  146.    mm->upper_skip = dest_height_sub;
  147.  
  148.    bmf = bmfa_MatchMaxFont(mm->lf_height);
  149.    dest_height_sub += mm->legend_rows*bmf_GetHeight(bmf);
  150.    dest_height_sub += bmf_GetHeight(bmf)/2;
  151.    mm->lower_skip = dest_height_sub-mm->upper_skip;
  152.  
  153.    mm->x_skip = mm->dpi/50L;
  154.  
  155.    mm->curr_width = mm->map->max_x-mm->map->min_x;
  156.    mm->curr_height = mm->map->max_y-mm->map->min_y;
  157.  
  158.    mm->ratio_num = mm->dest_width-mm->x_skip*2;
  159.    mm->ratio_den = mm->curr_width;
  160.  
  161.    if ( mm->ratio_num*mm->curr_height >
  162.       mm->ratio_den*(mm->dest_height-dest_height_sub) )
  163.    {
  164.       mm->ratio_num = mm->dest_height-dest_height_sub;
  165.       mm->ratio_den = mm->curr_height;
  166.    }
  167.  
  168.    mm->x_offset = mm->map->min_x;
  169.    mm->y_offset = mm->map->min_y;
  170.  
  171.    if ( mm->mgr != NULL )
  172.       mgr_Close(mm->mgr);
  173.  
  174.    mm->mgr = mgr_Open(
  175.       mm->dest_width,
  176.       (((mm->curr_height)*mm->ratio_num)/mm->ratio_den)+dest_height_sub );
  177.    if ( mm->mgr == NULL )
  178.       return 0;
  179.    mgr_SetFontHeight(mm->mgr, 7);
  180.  
  181.    mgr_SetLine(mm->mgr,
  182.       0,                             mm->lower_skip,
  183.       mm->dest_width-1, mm->lower_skip);
  184.    mgr_SetLine(mm->mgr,
  185.       0,                             mm->lower_skip,
  186.       0,                             mm->mgr->height-1-mm->upper_skip);
  187.    mgr_SetLine(mm->mgr,
  188.       mm->dest_width-1, mm->mgr->height-1-mm->upper_skip,
  189.       0,                             mm->mgr->height-1-mm->upper_skip);
  190.    mgr_SetLine(mm->mgr,
  191.       mm->dest_width-1, mm->mgr->height-1-mm->upper_skip,
  192.       mm->dest_width-1, mm->lower_skip);
  193.    return 1;
  194. }
  195.  
  196. void mgrmap_DrawTitle(mgrmap_type mm, char *title)
  197. {
  198.    map_sprite_struct *sprite;
  199.    int is_level_exit = 0;
  200.    int is_bonus_exit = 0;
  201.    long w, y;
  202.    short i;
  203.    char s[80];
  204.  
  205.    mgr_SetFontHeight(mm->mgr, mm->tf_height);
  206.    y = mm->mgr->height-1-mm->upper_skip+mgr_GetFontHeight(mm->mgr)/2;
  207.    mgr_SetString( mm->mgr, 0, y, title );
  208.    w = mgr_GetStringWidth(mm->mgr, title );
  209.    w += mgr_GetStringWidth(mm->mgr, "    ");
  210.  
  211.    /* search for nukem buttons */
  212.    mgr_SetFontHeight(mm->mgr, mm->lf_height);
  213.    for( i = 0; i < mm->map->sprite_cnt; i++ )
  214.    {
  215.       sprite = mm->map->sprite_list+i;
  216.       if ( sprite->picnum == NUKEBUTTON )
  217.       {
  218.          if ( sprite->lotag == 32767 || sprite->lotag < 0)
  219.          {
  220.             is_level_exit = 1;
  221.          }
  222.          else
  223.          {
  224.             is_level_exit = 1;
  225.             is_bonus_exit = 1;
  226.             sprintf(s, "exit to %hd", sprite->lotag);
  227.             mgr_SetString( mm->mgr, w, y, s );
  228.             w += mgr_GetStringWidth(mm->mgr, s );
  229.          }
  230.       }
  231.    }
  232.    if ( is_level_exit == 0 )
  233.    {
  234.       sprintf(s, "no level exit    ");
  235.       mgr_SetString( mm->mgr, w, y, s );
  236.    }
  237. }
  238.  
  239. void mgrmap_DrawLegend(mgrmap_type mm)
  240. {
  241.    long col_width;
  242.    long col, row;
  243.    int idx;
  244.    map_cnt_sprites(mm->map);
  245.    mgr_SetFontHeight(mm->mgr, mm->lf_height);
  246.  
  247.    col_width = mm->legend_f1_size+mm->legend_f2_size+mm->legend_f3_size;
  248.  
  249.    idx = 0;
  250.  
  251.    col = 0;
  252.    row = 1;
  253.    while( map_get_item_typ(idx) >= 0 )
  254.    {
  255.       if ( map_get_item_cnt(idx) > 0 )
  256.       {
  257.          static char s[80];
  258.  
  259.          sprintf(s, "%ldx", map_get_item_cnt(idx));
  260.          mgr_SetString( mm->mgr,
  261.             col*col_width,
  262.             mm->lower_skip-row*mgr_GetFontHeight(mm->mgr),
  263.             s );
  264.  
  265.          sprintf(s, "%s", map_get_item_id(idx));
  266.          mgr_SetString( mm->mgr,
  267.             col*col_width+mm->legend_f1_size,
  268.             mm->lower_skip-row*mgr_GetFontHeight(mm->mgr),
  269.             s );
  270.  
  271.          sprintf(s, "%s", map_get_item_string(idx));
  272.          mgr_SetString( mm->mgr,
  273.             col*col_width+mm->legend_f1_size+mm->legend_f2_size,
  274.             mm->lower_skip-row*mgr_GetFontHeight(mm->mgr),
  275.             s );
  276.  
  277.          row++;
  278.          if ( row > mm->legend_rows )
  279.          {
  280.             row = 1;
  281.             col++;
  282.          }
  283.       }
  284.       idx++;
  285.    }
  286.  
  287.  
  288. }
  289.  
  290.  
  291. void mgrmap_DrawWall(mgrmap_type mm, short wallidx)
  292. {
  293.    short wallidx2;
  294.    long x1, y1, x2, y2;
  295.  
  296.    x1 = mm->map->wall_list[wallidx].x;
  297.    y1 = mm->map->wall_list[wallidx].y;
  298.  
  299.    wallidx2 = mm->map->wall_list[wallidx].point2;
  300.    x2 = mm->map->wall_list[wallidx2].x;
  301.    y2 = mm->map->wall_list[wallidx2].y;
  302.  
  303.    if ( mm->map->wall_list[wallidx].nextsector < 0 ||
  304.         mm->map->wall_list[wallidx].nextwall < 0 )
  305.    {
  306.       long r;
  307.       r = mm->dpi/150L;
  308.       /*
  309.       if ( r == 0 )
  310.          r = 1;
  311.       */
  312.       mgr_MakeDiskBrush(mm->mgr, r);
  313.       mgr_SetLineBrush(mm->mgr,
  314.          mgrmap_XToMap(mm, x1),
  315.          mgrmap_YToMap(mm, y1),
  316.          mgrmap_XToMap(mm, x2),
  317.          mgrmap_YToMap(mm, y2) );
  318.    }
  319.    else
  320.    {
  321.       long r;
  322.       r = mm->dpi/400;
  323.       mgr_MakeDiskBrush(mm->mgr, r);
  324.       mgr_SetLineBrush(mm->mgr,
  325.          mgrmap_XToMap(mm, x1),
  326.          mgrmap_YToMap(mm, y1),
  327.          mgrmap_XToMap(mm, x2),
  328.          mgrmap_YToMap(mm, y2) );
  329.    }
  330. }
  331.  
  332. void mgrmap_DrawSector(mgrmap_type mm, short secnum)
  333. {
  334.    short i, wallnum, wallidx, wallidx2;
  335.    /* long x1, y1, x2, y2; */
  336.  
  337.    assert( mm != NULL );
  338.    assert( mm->map != NULL );
  339.    assert( mm->mgr != NULL );
  340.  
  341.    wallnum = mm->map->sec_list[secnum].wallnum;
  342.    wallidx = mm->map->sec_list[secnum].wallptr;
  343.    /*
  344.    x1 = mm->map->wall_list[wallidx].x;
  345.    y1 = mm->map->wall_list[wallidx].y;
  346.    */
  347.    for( i = 0; i < wallnum; i++ )
  348.    {
  349.       mgrmap_DrawWall(mm, wallidx);
  350.       wallidx2 = mm->map->wall_list[wallidx].point2;
  351.       /*
  352.       x2 = mm->map->wall_list[wallidx2].x;
  353.       y2 = mm->map->wall_list[wallidx2].y;
  354.  
  355.       if ( mm->map->wall_list[wallidx].nextsector < 0 ||
  356.            mm->map->wall_list[wallidx].nextwall < 0 )
  357.       {
  358.          mgr_MakeDiskBrush(mm->mgr, 2);
  359.          mgr_SetLineBrush(mm->mgr,
  360.             mgrmap_XToMap(mm, x1),
  361.             mgrmap_YToMap(mm, y1),
  362.             mgrmap_XToMap(mm, x2),
  363.             mgrmap_YToMap(mm, y2) );
  364.       }
  365.       else
  366.       {
  367.          mgr_SetLine(mm->mgr,
  368.             mgrmap_XToMap(mm, x1),
  369.             mgrmap_YToMap(mm, y1),
  370.             mgrmap_XToMap(mm, x2),
  371.             mgrmap_YToMap(mm, y2) );
  372.       }
  373.       */
  374.       wallidx = wallidx2;
  375.       /*
  376.       x1 = x2;
  377.       y1 = y2;
  378.       */
  379.    }
  380. }
  381.  
  382. void mgrmap_DrawLines(mgrmap_type mm)
  383. {
  384.    short i;
  385.    assert( mm != NULL );
  386.    assert( mm->map != NULL );
  387.    /*
  388.    for( i = 0; i < mm->map->sec_cnt; i++ )
  389.    {
  390.       mgrmap_DrawSector(mm, i);
  391.       printf("sector %hd/%hd\r", i, mm->map->sec_cnt );
  392.    }
  393.    */
  394.    for( i = 0; i < mm->map->wall_cnt; i++ )
  395.    {
  396.       mgrmap_DrawWall(mm, i);
  397.       printf("wall %hd/%hd\r", i+1, mm->map->wall_cnt ); fflush(stdout);
  398.    }
  399.  
  400. }
  401.  
  402. void mgrmap_DrawSprites(mgrmap_type mm)
  403. {
  404.    int idx, cnt;
  405.    char s[20];
  406.    map_sprite_struct *sprite;
  407.    short i;
  408.    assert( mm != NULL );
  409.    assert( mm->map != NULL );
  410.    mgr_SetFontHeight(mm->mgr, mm->idf_height);
  411.    cnt = 1;
  412.    for( i = 0; i < mm->map->sprite_cnt; i++ )
  413.    {
  414.       sprite = mm->map->sprite_list+i;
  415.       idx = map_get_info_idx(sprite->picnum, sprite->pal);
  416.       if ( idx >= 0 )
  417.       {
  418.          long w;
  419.  
  420.          if ( map_get_item_typ(idx) == MAP_TYP_MONSTER &&
  421.               mm->is_monster == 0 )
  422.             continue;
  423.  
  424.          if ( map_get_item_typ(idx) == MAP_TYP_CARD &&
  425.               mm->is_card == 0 )
  426.             continue;
  427.  
  428.          /* sprintf(s, "%hd", sprite->picnum); */
  429.          sprintf(s, "%s", map_get_item_id(idx));
  430.          w = mgr_GetStringWidth(mm->mgr, s);
  431.          mgr_SetString( mm->mgr,
  432.             mgrmap_XToMap(mm, sprite->x)-w/2,
  433.             mgrmap_YToMap(mm, sprite->y),
  434.             s
  435.          );
  436.          printf("sprite %d            \r", cnt++ ); fflush(stdout);
  437.       }
  438.       /*
  439.       else if ( sprite->picnum >= 1240 )
  440.       {
  441.          sprintf(s, "%hd", sprite->picnum);
  442.          mgr_SetString( mm->mgr,
  443.             mgrmap_XToMap(mm, sprite->x),
  444.             mgrmap_YToMap(mm, sprite->y),
  445.             s
  446.          );
  447.       }
  448.       */
  449.    }
  450. }
  451.